<script type="text/javascript">
  function uptotalitem(){
    document.getElementById('total_item').value = parseInt(document.getElementById('total_item').value) + 1;
  }

  function removeElement(parentDiv, childDiv){
     if (childDiv == parentDiv) {
          alert("The parent div cannot be removed.");
     }
     else if (document.getElementById(childDiv)) {
          var child = document.getElementById(childDiv);
          var parent = document.getElementById(parentDiv);
          parent.removeChild(child);
     }
     else {
          alert("Child div has already been removed or does not exist.");
          return false;
     }
  }

  function showspinner(id){
    var el = document.getElementById(id);
    el.style.display = '';
  }

  function showhidearea(s1,s2,s3){
    var el1 = document.getElementById(s1);
    var el2 = document.getElementById(s2);
    var el3 = document.getElementById(s3);
    if(el1.checked==true){
      el2.style.display = 'none';
      el3.style.display = 'none';
    }else{
      el2.style.display = '';
      el3.style.display = '';
    }
  }

  function recheck_all(){
    <%no = 0%>
    <%@running_stocks.each_with_index do |rs,i|%>
      <%no = i+1%>
      <%if rs.stock_type=='S'%>
        <%s_stock = Stock.find(:first, :conditions=>["id=?",rs.stock_id])%>
        <%if s_stock%>
          new Ajax.Updater('chartarea_<%=no%>', '/project/show_validate_chart2?no=<%=no%>&stock_type=<%=rs.stock_type%>', {asynchronous:true, evalScripts:true, parameters:'id='+$('data_<%=no%>').value+'&ed='+$('date_<%=no%>').value+'&min_est='+$('min_est').value+'&max_est='+$('max_est').value+'&min_event='+$('min_event').value+'&max_event='+$('max_event').value+'&leadlag_size='+$('leadlag_size').value});
        <%end%>
      <%else%>
        <%u_stock = UserStock.find(:first, :conditions=>["id=?",rs.user_stock_id])%>
        <%if u_stock%>
          new Ajax.Updater('chartarea_<%=no%>', '/project/show_validate_chart2?no=<%=no%>&stock_type=<%=rs.stock_type%>', {asynchronous:true, evalScripts:true, parameters:'id='+$('data_<%=no%>').value+'&ed='+$('date_<%=no%>').value+'&min_est='+$('min_est').value+'&max_est='+$('max_est').value+'&min_event='+$('min_event').value+'&max_event='+$('max_event').value+'&leadlag_size='+$('leadlag_size').value});
        <%end%>
      <%end%>
    <%end%>
    var i=0;
    var total_save = <%=no+1%>;
    var total_item = parseInt(document.getElementById('total_item').value);
    for(i=total_save;i<=total_item;i++){
      var no = ""+i;
      try{
        new Ajax.Updater('chartarea_'+no, '/project/show_validate_chart2?no='+no+'&stock_type='+$('stocktype_'+no).value, {asynchronous:true, evalScripts:true, parameters:'id='+$('data_'+no).value+'&ed='+$('date_'+no).value+'&min_est='+$('min_est').value+'&max_est='+$('max_est').value+'&min_event='+$('min_event').value+'&max_event='+$('max_event').value+'&leadlag_size='+$('leadlag_size').value});
      }catch(er){}
    }
  }

</script>
<link href="/stylesheets/tabheader.css" rel="stylesheet" type="text/css" />
<table width="100%" class="noborder_padding">
  <tr>
    <td width="200px" valign="top">
      <%=render :partial=>"sidemenu"%>
    </td>
    <td valign="top" style="padding-left: 10px;">
      <%=render :partial=>"tabheader"%>
      <h3><font color="#804040">Customizing Data Series & Windows</font><%if @word%><font color="red"><i> &nbsp;&nbsp;&nbsp; "<%=@word%>"</i></font><%end%></h3>
      <form action="/project/data_page_save" method="post" name="dataform">
      Windows &nbsp;&nbsp;&nbsp; (<i>Latest Update : </i><font color="red"><%=@project_setting.updated_at%></font>)<hr/>
      <table class="noborder" width="100%">
        <tr>
          <td></td>
          <td align="center">Min</td>
          <td></td>
          <td align="center">Max</td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td width="200px">Estimation Window</td>
          <td width="50px"><input type="text" name="setting[min_est]" id="min_est" value="<%=@project_setting.min_est%>" style="width:50px;"/></td>
          <td width="10px" align="center">,</td>
          <td width="50px"><input type="text" name="setting[max_est]" id="max_est" value="<%=@project_setting.max_est%>" style="width:50px;"/></td>
          <td><font color="blue"> &nbsp;&nbsp; Must be nagative value (Event Date = 0)</font></td>
          <td rowspan="3" align="center" valign="middle">
            <%if @running_stocks.size!=0%>
              <input type="button" value="Check" onclick="recheck_all();"/>
            <%end%>
          </td>
        </tr>
        <tr>
          <td>Event Window</td>
          <td><input type="text" name="setting[min_event]" id="min_event" value="<%=@project_setting.min_event%>" style="width:50px;"/></td>
          <td align="center">,</td>
          <td><input type="text" name="setting[max_event]" id="max_event" value="<%=@project_setting.max_event%>" style="width:50px;"/></td>
          <td><font color="blue"> &nbsp;&nbsp; Min Event = Max Estimation + 1 , Min &le; 0 &le; Max</font></td>
        </tr>
        <tr>
          <td>Validate Lead/Lag</td>
          <td colspan="3" align="center">&#177; <input type="text" name="setting[leadlag_size]" id="leadlag_size" value="<%=@project_setting.leadlag_size%>" style="width:50px;"/></td>
          <td><font color="blue"> &nbsp;&nbsp; ...</font></td>
        </tr>
      </table><br/>
      <input type="hidden" name="total_item" id="total_item" value="<%=@running_stocks.size%>" style="width:50px;"/>
      Data &nbsp;&nbsp;&nbsp; (<i>Total : </i><font color="red"><%=@running_stocks.size%></font>)<hr/>
      <div id="dataarea">
        <%@running_stocks.each_with_index do |rs,i|%>
          <%no = i+1%>
          <%if rs.stock_type=='S'%>
            <%stock = Stock.find(:first, :conditions=>["id=?",rs.stock_id])%>
            <%if stock%>
            <div id="dataarea_<%=no%>">
              <table width="100%" border="1">
                <tr>
                  <td width="20px" valign="middle"><input type="button" value="X" onclick="removeElement('dataarea','dataarea_<%=no%>');"/></td>
                  <td height="60px" valign="middle" align="right">
                    <input type="hidden" name="stock_type_<%=no%>" value="S"/>
                    <%= link_to_record_select 'Select', 'stocks', :onselect=>"new Ajax.Updater('tablearea_#{no}', '/project/selected_system_stock/'+id+'?no=#{no}', {asynchronous:true, evalScripts:true})"%>
                    <br/>
                    System's Data
                  </td>
                  <td width="620px">
                    <span id="tablearea_<%=no%>">
                      <table width="100%" border="0">
                        <tr>
                          <td align="right"><input type="hidden" id="data_<%=no%>" name="data_<%=no%>" value="<%=stock.id%>"/><%=stock.s_symbol%>
                            <br/>
                            <input type="checkbox" name="check_<%=no%>" id="check_<%=no%>" value="Y" <%if rs.rm_flag=='Y'%>checked<%end%> onclick="showhidearea('check_<%=no%>','calarea_<%=no%>','chartarea_<%=no%>');"/> Market
                          </td>
                          <td width="40px" valign="middle" align="center"><a href="/project/price_return_chart/<%=stock.id%>?stock_type=S" target="_blank"><img src="/images/graph-icon.png" border="0" width="30px" alt="Price & Return"/></a></td>
                          <td width="100px" valign="middle" align="center">
                            <span id="calarea_<%=no%>" <%if rs.rm_flag=='Y'%>style="display:none;"<%end%>>
                            Event Date<br/>
                            <input type="text" style="width:70px;" name="date_<%=no%>" id="date_<%=no%>" <%if rs.event_date%>value="<%=rs.event_date%>"<%end%>/>
                            <img src="/images/calendar.png" border="0" id="trigger_<%=no%>" style="cursor: pointer;" title="Date selector"/>
                            <%
                              #min_year,max_year = get_minmax_year(stock.id, 'S')
                            %>
                            <script type="text/javascript">
                              Calendar.setup({
                                inputField  : "date_<%=no%>",
                                button      : "trigger_<%=no%>",
                                ifFormat    : "%Y-%m-%d",
                                daFormat    : "%Y-%m-%d",
                                //range       : [<%#=min_year%>,<%#=max_year%>],
                                singleClick : true
                              });
                            </script>
                            </span>
                          </td>
                          <td width="120px" align="right">
                            Before D/E: <input type="text" name="est_de_<%=no%>" value="<%=rs.est_de%>" style="width:40px;"/><br/>
                            After D/E: <input type="text" name="event_de_<%=no%>" value="<%=rs.event_de%>" style="width:40px;"/>
                          </td>
                          <td width="280px" valign="middle" align="center">
                            <span id="chartarea_<%=no%>" <%if rs.rm_flag=='Y'%>style="display:none;"<%end%>>
                              <!--span id="spinner_<%=no%>"><img src="/images/spinner.gif"/></span-->
                            </span>
                          </td>
                        </tr>
                      </table>
                    </span>
                  </td>
                </tr>
              </table>
            </div>
            <%end%>
          <%else%>
            <%stock = UserStock.find(:first, :conditions=>["id=?",rs.user_stock_id])%>
            <%if stock%>
            <div id="dataarea_<%=no%>">
              <table width="100%" border="1">
                <tr>
                  <td width="20px" valign="middle"><input type="button" value="X" onclick="removeElement('dataarea','dataarea_<%=no%>');"/></td>
                  <td height="60px" valign="middle" align="right">
                    <input type="hidden" name="stock_type_<%=no%>" value="U"/>
                    <%= link_to_record_select 'Select', 'user_stocks', :onselect=>"new Ajax.Updater('tablearea_#{no}', '/project/selected_user_stock/'+id+'?no=#{no}', {asynchronous:true, evalScripts:true})"%>
                    <br/>
                    Your Data
                  </td>
                  <td width="620px">
                    <span id="tablearea_<%=no%>">
                      <table width="100%" border="0">
                        <tr>
                          <td align="right"><input type="hidden" id="data_<%=no%>" name="data_<%=no%>" value="<%=stock.id%>"/><%=stock.s_symbol%>
                            <br/>
                            <input type="checkbox" name="check_<%=no%>" id="check_<%=no%>" value="Y" <%if rs.rm_flag=='Y'%>checked<%end%> onclick="showhidearea('check_<%=no%>','calarea_<%=no%>','chartarea_<%=no%>');"/> Market
                          </td>
                          <td width="40px" valign="middle" align="center"><a href="/project/price_return_chart/<%=stock.id%>?stock_type=U" target="_blank"><img src="/images/graph-icon.png" border="0" width="30px" alt="Price & Return"/></a></td>
                          <td width="100px" valign="middle" align="center">
                            <span id="calarea_<%=no%>" <%if rs.rm_flag=='Y'%>style="display:none;"<%end%>>
                            Event Date<br/>
                            <input type="text" style="width:70px;" name="date_<%=no%>" id="date_<%=no%>" <%if rs.event_date%>value="<%=rs.event_date%>"<%end%>/>
                            <img src="/images/calendar.png" border="0" id="trigger_<%=no%>" style="cursor: pointer;" title="Date selector"/>
                            <%
                              #min_year,max_year = get_minmax_year(stock.id, 'U')
                            %>
                            <script type="text/javascript">
                              Calendar.setup({
                                inputField  : "date_<%=no%>",
                                button      : "trigger_<%=no%>",
                                ifFormat    : "%Y-%m-%d",
                                daFormat    : "%Y-%m-%d",
                                //range       : [<%#=min_year%>,<%#=max_year%>],
                                singleClick : true
                              });
                            </script>
                            </span>
                          </td>
                          <td width="120px" align="right">
                            Before D/E: <input type="text" name="est_de_<%=no%>" value="<%=rs.est_de%>" style="width:40px;"/><br/>
                            After D/E: <input type="text" name="event_de_<%=no%>" value="<%=rs.event_de%>" style="width:40px;"/>
                          </td>
                          <td width="280px" valign="middle" align="center">
                            <span id="chartarea_<%=no%>" <%if rs.rm_flag=='Y'%>style="display:none;"<%end%>>
                              <!--span id="spinner_<%=no%>"><img src="/images/spinner.gif"/></span-->
                            </span>
                          </td>
                        </tr>
                      </table>
                    </span>
                  </td>
                </tr>
              </table>
            </div>
            <%end%>
          <%end%>
        <%end%>
        <%if @running_stocks.size!=0%>
          <script type="text/javascript">
            //recheck_all();
          </script>
        <%end%>
      </div>
      <input type="button" value="Add System's" onclick="uptotalitem(); <%=remote_function(:update=>"dataarea", :position=>"bottom", :url=>{:action=>:add_data}, :with=>"'no='+$('total_item').value")%>"/>
      <input type="button" value="Add Yours" onclick="uptotalitem(); <%=remote_function(:update=>"dataarea", :position=>"bottom", :url=>{:action=>:add_userdata}, :with=>"'no='+$('total_item').value")%>"/>
      <br/>
      <font color="blue">
        * You can manage your data series at "DataStore" menu.
        <br/>
        ** SET Index(market) available: 2001-2011
        <br/>
        *** In some case, You may pick 1 of your or systems's data as market
      </font>
      <center><input type="submit" value="Save"/></center>
      </form>
    </td>
  </tr>
</table>